<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Partytown Test Page" />
    <title>Integration Event Forwarding</title>

    <script>
      partytown = {
        forward: [
          'superDuperFunction',
          'superArray.push',
          'KiwiSizing',
          ['superPreservedArray.unshift', { preserveBehavior: true }],
        ],
        logCalls: true,
        logGetters: true,
        logSetters: true,
        logStackTraces: false,
        logScriptExecution: true,
      };
    </script>
    <script src="/~partytown/debug/partytown.js"></script>

    <script>
      superDuperFunction('fnReady');
      superArray.push('arrayReady');
      superPreservedArray.unshift('arrayReady');
    </script>

    <script type="text/partytown">
      (function() {
        let inc = 0;

        const testFn = document.getElementById('testFn');

        superDuperFunction = function() {
          const testFnInner = document.getElementById('testFnInner');
          switch (arguments[0]) {
            case 'fnReady': {
              testFn.textContent = arguments[0];
              testFnInner.textContent = arguments[0];
              break;
            }
            case 'testForwardEvent': {
              testFn.textContent = arguments[1] + (++inc);
              testFnInner.textContent = arguments[1] + (inc);
              break;
            }
          }
        };
      })();
    </script>

    <script type="text/partytown">
      (function() {
        const testArray = document.getElementById('testArray');

        superArray = [];
        superArray.push = function() {
          const testArrayInner = document.getElementById('testArrayInner');
          switch (arguments[0]) {
            case 'arrayReady': {
              testArray.textContent = arguments[0];
              testArrayInner.textContent = arguments[0];
              break;
            }
            default: {
              testArray.textContent = JSON.stringify(arguments[0]);
              testArrayInner.textContent = JSON.stringify(arguments[0]);
              break;
            }
          }
        }
      })();
    </script>

    <script type="text/partytown">
      (function() {
        const testArray = document.getElementById('testPreservedArray');
        const label = document.getElementById('testPreservedArrayReturnValue');

        superPreservedArray = [];
        superPreservedArray.unshift = function() {
          switch (arguments[0]) {
            case 'arrayReady': {
              testArray.textContent = arguments[0];
              label.textContent = 1;
              break;
            }
            default: {
              testArray.textContent = JSON.stringify(arguments[0]);
              break;
            }
          }
        }
      })();
    </script>

    <script type="text/partytown">
      (function () {
        window.KiwiSizing = window.KiwiSizing === undefined ? {} : window.KiwiSizing;
        KiwiSizing.data = {
          collections: "211448987800",
        }

        console.log("Before KiwiSizing", KiwiSizing)
        console.log("Before KiwiSizing.data", KiwiSizing.data)
      })();
    </script>
    <script type="text/partytown">
      console.log("KiwiSizing", KiwiSizing)
      console.log("KiwiSizing.data", KiwiSizing.data)
      window.document.getElementById("kiwisizing").innerText = JSON.stringify(KiwiSizing.data);
    </script>
    <style>
      body {
        font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif,
          Apple Color Emoji, Segoe UI Emoji;
        font-size: 12px;
      }
      h1 {
        margin: 0 0 15px 0;
      }
      ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
      }
      a {
        display: block;
        padding: 16px 8px;
      }
      a:link,
      a:visited {
        text-decoration: none;
        color: blue;
      }
      a:hover {
        background-color: #eee;
      }
      li {
        display: flex;
        margin: 15px 0;
      }
      li strong,
      li code,
      li span,
      li button {
        white-space: nowrap;
        flex: 1;
        margin: 0 5px;
      }
    </style>
  </head>
  <body>
    <h1>Integration Event Forwarding</h1>
    <ul>
      <li>
        <strong>fn event</strong>
        <button id="buttonForwardEvent">fn</button>
        <code id="testFn"></code>
        <code id="testFnInner"></code>
        <script>
          (function () {
            const btn = document.getElementById('buttonForwardEvent');
            btn.addEventListener('click', function (ev) {
              superDuperFunction('testForwardEvent', ev.type);
            });
          })();
        </script>
      </li>

      <li>
        <strong>array push event</strong>
        <button id="buttonArrayPush">push</button>
        <code id="testArray"></code>
        <code id="testArrayInner"></code>
        <script>
          (function () {
            let mph = 88;
            const btn = document.getElementById('buttonArrayPush');
            btn.addEventListener('click', function (ev) {
              superArray.push({ mph: mph++ });
            });
          })();
        </script>
      </li>
      <li>
        <strong>array unshift event with preserve behavior</strong>
        <button id="buttonPreservedArrayPush">unshift</button>
        <span>
          <code id="testPreservedArray"></code>
          <code id="testPreservedArrayReturnValue"></code>
        </span>
        <script>
          (function () {
            let mph = 88;
            const btn = document.getElementById('buttonPreservedArrayPush');
            const label = document.getElementById('testPreservedArrayReturnValue');
            btn.addEventListener('click', function (ev) {
              label.textContent = superPreservedArray.unshift({ mph: mph++ });
            });
          })();
        </script>
      </li>
      <li>
        <strong>Kiwi sizing</strong>
        <code id="kiwisizing"></code>
      </li>
    </ul>

    <script type="text/partytown">
      document.body.classList.add('completed');
    </script>

    <hr />
    <p><a href="/tests/">All Tests</a></p>
    <hr />
  </body>
</html>
